package com.zonski.jbots.desktop.editer.tool;

import com.zonski.jbots.desktop.editer.AbstractMouseTool;
import com.zonski.jbots.engine.Room;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created by IntelliJ IDEA.
 * User: Chris Glover
 * Date: Oct 27, 2003
 * Time: 2:56:19 PM
 * To change this template use Options | File Templates.
 */
public class TileTool extends AbstractMouseTool
{
    private int tileId;

    public TileTool(int tileId)
    {
        this.tileId = tileId;
    }

    public MouseListener createMouseListener()
    {
        return new CreateMouseListener();
    }

    public void createAt(int mx, int my)
    {
        // recreate the tile array
        Room room = this.getContext().getRoom();
        if(room.getWidth() > mx && room.getHeight() > my)
        {
            int tx = mx / room.getTileSize();
            int ty = my / room.getTileSize();

            int[][] tiles = new int[room.getNumberOfTilesDown()][room.getNumberOfTilesAcross()];
            for(int y=0; y<room.getNumberOfTilesDown(); y++)
            {
                for(int x = 0; x<room.getNumberOfTilesAcross(); x++)
                {
                    tiles[y][x] = room.getTileType(x, y);
                }
            }

            tiles[ty][tx] = this.tileId;
            room.setTiles(tiles);
            this.getContext().getComponent().repaint();
        }
    }

    private class CreateMouseListener extends MouseAdapter
    {
        public CreateMouseListener()
        {
        }

        public void mousePressed(MouseEvent event)
        {
            super.mousePressed(event);
            createAt(event.getX(), event.getY());
        }
    }
}
